package com.xlh.dto.courseTest;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xlh.dto.course.TemplateDataDTO;
import com.xlh.enums.courseTest.QuestionOriginEnum;
import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.pojo.courseTest.CourseQuestionRepository;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetails;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;

@Data
public class QuestionListDTO {
    private Long id;
    private String content;
    private Integer questionType;
    private String questionTypeMessage;
    private Long chapterId;
    private String chapterName;
    private Long courseId;
    private String courseName;
    private String questionOrigin;
    private Integer duplicatePercent;
    private List<CourseQuestionRepositoryDetails> answer;
    private String modelReport;
    private Boolean isPreset;

    private Boolean used;
    private Boolean chosen;

    @JsonIgnore
    private Long sourceQuestionId;
    @JsonIgnore
    private String path;

    private String jxContent;
    private String jxContentHtml;
    public QuestionListDTO convert(CourseQuestionRepository repository, List<CourseQuestionRepositoryDetails> detailsList, TemplateDataDTO template) {
        this.setId(repository.getId());
        this.setContent(repository.getMdContent());
        this.setQuestionType(repository.getQuestionType());
        this.setChapterId(repository.getChapterId());
        this.setAnswer(detailsList);
        if (template != null) {
            this.setModelReport(template.getFilePath());
        }
        this.setJxContent(repository.getJxContent());
        this.setJxContentHtml(repository.getJxContentHtml());
        return this;
    }

    public void convert(Map<Long, List<CourseQuestionRepositoryDetails>> detailsListMap, List<Long> userQuestionIds, List<Long> chosenIds) {
        this.convert(detailsListMap);

        this.setQuestionOrigin(
                this.getIsPreset() || this.getSourceQuestionId() != null ? QuestionOriginEnum.PRESET.getMessage() : QuestionOriginEnum.CUSTOM.getMessage()
        );

        if (CollectionUtils.isNotEmpty(userQuestionIds)) {
            this.setUsed(userQuestionIds.contains(this.getId()));
        } else {
            this.setUsed(false);
        }

        if (CollectionUtils.isNotEmpty(chosenIds)) {
            this.setChosen(chosenIds.contains(this.getId()));
        } else {
            this.setChosen(false);
        }
    }

    public void convert(Map<Long, List<CourseQuestionRepositoryDetails>> detailsListMap) {
        if (detailsListMap != null) {
            this.setAnswer(detailsListMap.get(this.getId()));
        }

        QuestionTypeEnum typeEnum = QuestionTypeEnum.forCode(this.getQuestionType());
        if (typeEnum != null) {
            this.setQuestionTypeMessage(typeEnum.getMessage());
        }
    }
}
